//
//  TJLoginController.m
//  BaoDian
//
//  Created by mac on 2024/7/17.
//

#import "TJLoginController.h"
#import "TJTitleTextField.h"
#import "QMUIButton.h"
#import "TJStyle1Button.h"
#import "TJAlertController.h"
 
@interface TJLoginController ()


@property (nonatomic, weak) TJTitleTextField *accountTextField;
@property (nonatomic, weak) TJTitleTextField *passwordTextField;
@property (nonatomic, weak) TJStyle1Button *loginButton;

@end

@implementation TJLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.orangeColor;
   
    UILabel *titleLabel = [UILabel lableWithSuperView:self.view font:UIFontBoldMake(24) color:UIColor.whiteColor title:[NSString stringWithFormat:@"欢迎登录%@", getAppName()] textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KStatusBarHight + 80);
        make.left.offset(32);
    }];
    
    UILabel *subTitleLabel = [UILabel lableWithSuperView:self.view font:UIFontMake(14) color:UIColor.whiteColor title:@"如果您还没账号，请联系推广专员创建账号。" textAlignment:NSTextAlignmentLeft];
    subTitleLabel.numberOfLines = 0;
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(8);
        make.left.equalTo(titleLabel);
        make.right.inset(leftRightMargin);
    }];

    TJTitleTextField *accountTextField = [TJTitleTextField addToView:self.view];
    accountTextField.titleLabel.text = @"登录账号";
    accountTextField.addCornerRadius(25);
    accountTextField.textField.maximumTextLength = 11;
    accountTextField.backgroundColor = UIColor.tj_1E1F23Color;
    accountTextField.textField.placeholder = @"请输入您的登录账号";
    accountTextField.textField.keyboardType = UIKeyboardTypeNumberPad;
    [accountTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(32);
        make.top.equalTo(titleLabel.mas_bottom).offset(60);
        make.height.offset(50);
    }];
    
    TJTitleTextField *passwordTextField = [TJTitleTextField addToView:self.view];
    passwordTextField.titleLabel.text = @"登录密码";
    passwordTextField.textField.placeholder = @"请输入您的账号密码";
    passwordTextField.addCornerRadius(25);
    passwordTextField.backgroundColor = UIColor.tj_1E1F23Color;
    passwordTextField.textField.keyboardType = UIKeyboardTypeASCIICapable;
    passwordTextField.textField.secureTextEntry = YES;
    [passwordTextField.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    passwordTextField.textField.maximumTextLength = 11;
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(accountTextField);
        make.top.equalTo(accountTextField.mas_bottom).offset(16);
    }];
    
    QMUIButton *forgetPasswordButton = [QMUIButton buttonWithSuperView:self.view font:UIFontMake(12) color:UIColorMake(0x2D80FF) title:@"忘记密码?"];
    [forgetPasswordButton addTarget:self action:@selector(didClickForgetPasswordButton) forControlEvents:UIControlEventTouchUpInside];
    [forgetPasswordButton sizeToFit];
    [forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordTextField.mas_bottom).offset(12);
        make.height.offset(forgetPasswordButton.tj_size.height);
        make.width.offset(forgetPasswordButton.tj_size.width);
        make.centerX.offset(0);
    }];
    
    TJStyle1Button *loginButton = [TJStyle1Button buttonWithSuperView:self.view font:UIFontMediumMake(16) color:nil title:@"登录"];
    loginButton.enabled = NO;
    [loginButton addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(buttonHeight);
        make.top.equalTo(forgetPasswordButton.mas_bottom).offset(46);
    }];
    
    _accountTextField = accountTextField;
    _passwordTextField = passwordTextField;
    _loginButton = loginButton;
}

- (void)didClickLoginButton {
    [self.view endEditing:YES];
    
    [TJHUD showLoadingInView:self.view];
    [TJMinePresent loginWithAccount:_accountTextField.textField.text password:_passwordTextField.textField.text complete:^(NSError * _Nullable error) {
        if (error) {
            if ([error.domain isEqualToString:ERRPRDOMAIN]) {
                    [TJAlertController alertInController:self title:@"登录失败" message:@"账号或密码错误，请重新输入" left:^(TJAlertAction * _Nonnull action) {
                        action.title = @"确认";
                    } right:nil];
            } else {
                [TJHUD showMessage:error.localizedFailureReason];
            }
        }
    }];
}

- (void)textFieldDidChange:(UITextField *)textField {
    _loginButton.enabled = !isEmptyString(_passwordTextField.textField.text) && !isEmptyString(_accountTextField.textField.text);
}
 

- (void)didClickForgetPasswordButton {
    [self.navigationController pushViewControllerWithName:@"HJForgetPasswordController"];
}

- (UIImage *)topBackgroundImage {
    return [UIImage imageNamed:@"loginTopBackground"];
}

- (BOOL)hiddenNavigationBar {
    return YES;
}
 

@end
